package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.service.TbGoodsService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference
    private TbGoodsService tbGoodsService;

    @RequestMapping("toList")
    public String toList() {
        return "/admin/goods";
    }

    @RequestMapping("getGoodsInfo")
    @ResponseBody
    public Result getGoodsInfo() {
        List<TbGoods> list = tbGoodsService.getGoodsInfos();
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

    @RequestMapping("updateStatus")
    @ResponseBody
    public Result updateStatus(String[] ids) {
        int i = tbGoodsService.updateStatus(ids, "1");
        if (i > 0) {
            System.out.println("舒适" + ids);
            rocketMQTemplate.convertAndSend("springboot-topic", ids);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    //审核通过
//    @RequestMapping("subStatus")
//    @ResponseBody
//    public Result subStatus(String[] ids){
//        try {
//            TbGoods goods = tbGoodsService.updateStatus(ids,"1");
//            if(Objects.equals(goods.getAuditStatus(),"2")){
//                rocketMQTemplate.convertAndSend("springboot-topic",ids);
//            }
//            return Result.build(ConstantEnum.SUCCESS);
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.build(ConstantEnum.ERROR);
//        }
//    }


}
